package dao

import (
	"ssims/models"
)

func GetAllRelation(relation map[string]interface{}) (error, []models.Relation, int64) {
	var relationData []models.Relation
	page := relation["page"].(int)
	pageSize := relation["limit"].(int)
	searchName := relation["searchName"].(string)
	var total int64
	err := db.Table("relation").Select("relation_no, relation_name1, relation_name2,  relation, update_time").Where("relation_name1 like ? and is_del = false", searchName+"%").Order("relation_name1 ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&relationData).Error
	return err, relationData, total
}

// Where("is_del = false").
func AddRelation(relation models.Relation) error {
	err := db.Table("relation").Select("relation_no", "relation_name1", "relation_name2", "relation", "update_time").Create(&relation).Error
	return err
}

func UpdateRelation(relation models.Relation) error {
	err := db.Table("relation").Where("relation_no = ?", relation.RelationNo).Updates(&relation).Error
	return err
}

func DelRelation(id string) error {
	// var Relation []models.Relation
	err := db.Table("relation").Where("relation_no = ?", id).Update("is_del", true).Error
	return err
}
